package com.dao.strategy;

import com.model.bo.DashOrder;
import com.model.po.Strategys;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface StrategysBdMapper {

    /*
   保存数据源策略
    */
    @Insert("insert into bi_db_strategys(bs_id, bs_db_id, bs_rule,rule_str, bs_name, is_open, create_date) values" +
            "(#{id}, #{tarId}, #{rule}, #{ruleStr}, #{name}, #{isOpen}, " +
            "to_date(to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),'YYYY-MM-DD hh24:mi:ss'))")
    @SelectKey(before=true,keyProperty="id",resultType=int.class,statement="SELECT BI_DB_STRATEGYS_SEQUENCE.nextval from dual",keyColumn = "bs_id")
    void addStrategys(Strategys strategys);

    /*
    更新数据源策略
     */
    @Update("<script>"+
            "UPDATE bi_db_strategys set "+
            "BS_DB_ID = #{tarId}" +
            "<if test=\"rule != null\"> , BS_RULE = #{rule} </if>" +
            "<if test=\"ruleStr != null\">, RULE_STR = #{ruleStr} </if> "+
            "<if test=\"name != null\"> , BS_NAME = #{name} </if>" +
            "<if test=\"createBy != null\"> , createBy = #{createBy} </if>" +
            "<if test=\"isOpen != null\"> , IS_OPEN = #{isOpen} </if>" +
            ",updata_date = to_date(to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),'YYYY-MM-DD hh24:mi:ss') "+
            " where bs_id = #{id}" +
            "</script>")
    void updataStrategys(Strategys strategys);

    /*
    删除数据源策略
     */
    @Delete("<script>" +
            "delete from bi_db_strategys where bs_id in" +
            "(" +
            "<foreach collection=\"list\" index=\"index\" item=\"item\"  separator=\",\">" +
            "#{item, jdbcType = NUMERIC}" +
            " </foreach>" +
            ")" +
            "</script>")
    void delStrategys(List<Integer> idList);

    /*
    查询数据源策略Id
     */
    @Select("select bs_id as id from bi_db_strategys where bs_db_id = #{id}")
    List<Integer> getStrIdList(int id);

    /*
   查询数据源策略
    */
    @Select(" select bs_id as id, bs_db_id as tarId, bs_name as name, is_open as isOpen,rule_Str as ruleStr, create_date as createDate,  " +
            " updata_date as updataDate from bi_db_strategys where bs_id = #{id}")
    Strategys getStrategys(int id);

    /*
    查询数据源策略对象的类型
     */
    @Select("select bo_type from bi_db_object where bo_st_id = #{id}")
    List<String> getStrategysType(int id);

    /*
    查询用户组id,name
     */
    @Select("select bg_id as id, bg_name as name from bi_user_groups, bi_db_object where bo_type = '0' " +
            "and bo_ob_id = bg_id and bo_st_id = #{id} ")
    List<DashOrder> getUserGroupIdAndName(int id);

    /*
     查询用户组id,name
      */
    @Select("select bu_id as id, bu_name as name from bi_users, bi_db_object where bo_type = '1' " +
            "and bo_ob_id = bu_id and bo_st_id = #{id} ")
    List<DashOrder> getUserIdAndName(int id);

    /*
    添加数据源策略对象
     */
    @Insert("insert into bi_db_object(bo_id, bo_st_id, bo_type, bo_ob_id) values" +
            "(#{id}, #{stId}, #{objectType}, #{obId})")
    @SelectKey(before=true,keyProperty="id",resultType=int.class,statement="SELECT BI_DB_OBJECT_SEQUENCE.nextval from dual",keyColumn = "bo_id")
    void addObject(@Param("stId") int stId, @Param("objectType") String objectType, @Param("obId") int obId);


    /*
    删除对象
     */
    @Delete("delete from bi_db_object where bo_st_id = #{stId}")
    void delObject(int stId);

    /*
    查询在某个数据源下的所有策略
     */
    @Select("select bs_rule from bi_db_strategys where bs_id in " +
            "(select bo_st_id from bi_db_object " +
            "where (bo_type = '0' and bo_ob_id in (select br_user_group from bi_user_rel_groups where br_user_id= #{userId})) " +
            " or (BO_TYPE='1' and bo_ob_id = #{userId} )) and bs_db_id = #{baseId} ")
    List<String> getSqlStr(@Param("userId") int userId, @Param("baseId") int baseId);
}
